package center;

import cn.hutool.core.util.StrUtil;
import common.ZkUtil;
import org.apache.zookeeper.*;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * Date: 2020-07-16
 * Time: 14:28
 *
 * @author zyh
 */
public class ZkTest {

    ZooKeeper zooKeeper;

    @Before
    public void init() {

        ZkConfig zkConfig = new ZkConfig();
        zkConfig.setConnectString("192.168.1.38:2181,192.168.1.39:2181,192.168.1.40:2181,192.168.1.41:2181");
        zkConfig.setSessionOutTime(4000);
        ZkUtil zkUtil = new ZkUtil(zkConfig);
        try {
            zooKeeper = zkUtil.getZookeeper();
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testConfig() throws InterruptedException, KeeperException {

        TestConfig testConfig = new TestConfig();
        WatcherCallback defaultWatcher = new WatcherCallback(zooKeeper, "/ipconfig", testConfig);
        defaultWatcher.await();

        while (true) {
            if (StrUtil.isBlank(testConfig.getIp())) {
                System.out.println("ip is null");
                defaultWatcher.await();
            } else {
                System.out.println("ip" + testConfig.getIp());
            }
            Thread.sleep(2000);
        }

    }
}
